package com.getpebble.android.core.auth.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.getpebble.android.PebbleApplication;
import com.getpebble.android.R;
import com.getpebble.android.common.core.trace.Analytics.MobileAppBehavior;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.framework.fragment.PblBaseFragment;
import com.getpebble.android.core.auth.activity.LoginActivity;
import com.getpebble.android.main.activity.MainActivity;

public class LoginFragment extends PblBaseFragment {
    private static final String TAG = LoginFragment.class.getSimpleName();
    private Button existingUserButton;
    private ConnectivityManager mConnectivityManager;
    private TextView mTermsAndConditions;
    private Button newUserButton;

    public int getLayoutId() {
        return R.layout.fragment_login;
    }

    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.mConnectivityManager = (ConnectivityManager) getActivity().getSystemService("connectivity");
        this.newUserButton = (Button) container.findViewById(R.id.newPebbleUserSelection);
        this.newUserButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MobileAppBehavior.logButtonTapped("NewUser", "Login");
                NetworkInfo networkInfo = LoginFragment.this.mConnectivityManager.getActiveNetworkInfo();
                if (networkInfo == null || !networkInfo.isConnected()) {
                    LoginFragment.this.getLoginActivity().switchToNoConnectivityFragment(LoginFragment.this);
                    return;
                }
                String signUpUrl = PebbleApplication.getBootConfig().getAuthenticationSignUpUrl();
                Trace.info(LoginFragment.TAG, "signUpUrl = " + signUpUrl);
                LoginFragment.this.getLoginActivity().switchToLoginWebFragment(signUpUrl);
            }
        });
        this.existingUserButton = (Button) container.findViewById(R.id.existingPebbleUserSelection);
        this.existingUserButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MobileAppBehavior.logButtonTapped("HaveAccount", "Login");
                NetworkInfo networkInfo = LoginFragment.this.mConnectivityManager.getActiveNetworkInfo();
                if (PebbleApplication.getSessionManager().fetchUserAccount() != null) {
                    Activity activity = LoginFragment.this.getActivity();
                    if (activity != null) {
                        Intent intent = new Intent(activity, MainActivity.class);
                        intent.addFlags(268500992);
                        activity.startActivity(intent);
                        activity.finish();
                    }
                } else if (networkInfo == null || !networkInfo.isConnected()) {
                    LoginFragment.this.getLoginActivity().switchToNoConnectivityFragment(LoginFragment.this);
                } else {
                    String signInUrl = PebbleApplication.getBootConfig().getAuthenticationSignInUrl();
                    Trace.info(LoginFragment.TAG, "signInUrl = " + signInUrl);
                    LoginFragment.this.getLoginActivity().switchToLoginWebFragment(signInUrl);
                }
            }
        });
        this.mTermsAndConditions = (TextView) container.findViewById(R.id.loginClickTermsConditions);
        this.mTermsAndConditions.setPaintFlags(this.mTermsAndConditions.getPaintFlags() | 8);
        this.mTermsAndConditions.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                MobileAppBehavior.logButtonTapped("TermsAndConditions", "Login");
                LoginFragment.this.getActivity().startActivity(new Intent("android.intent.action.VIEW", Uri.parse(PebbleApplication.getBootConfig().getTermsAndConditionsUrl())));
            }
        });
    }

    private LoginActivity getLoginActivity() {
        return (LoginActivity) getActivity();
    }
}
